package com.flca.frw.zk.validate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Constraint;

import com.flca.frw.util.TransformUtils;
import com.flca.frw.validators.ValidationError;
import com.flca.frw.validators.Validator;

public class ValidateDate implements Constraint {

	private Date minDate;
	private Date maxDate;
	private boolean req;

	public ValidateDate(Date minDate, Date maxDate, boolean aRequired) 
	{
		super();
		req = aRequired;
		this.minDate = minDate;
		this.maxDate = maxDate;
	}

	public void validate(Component aComp, Object aValue) 	throws WrongValueException 
	{
		Date testDate = (Date) aValue;
		
		if (testDate != null) {
			if (req) {
				String msg = "Dit veld is verplicht"; 
				throw new WrongValueException(aComp, msg);
			}
			
			List<ValidationError> errors = new ArrayList<ValidationError>();
			if (!Validator.testGreaterOrEquals(testDate, minDate, errors, "", null)) {
				TransformUtils tu = TransformUtils.getInstance();
				String msg = "Datum te oud, minimum = " + tu.date2String(minDate); 
				throw new WrongValueException(aComp, msg);
			}
			if (!Validator.testSmallerOrEquals(testDate, maxDate, errors, "", null)) {
				TransformUtils tu = TransformUtils.getInstance();
				String msg = "Datum te nieuw, mamimum = " + tu.date2String(maxDate); 
				throw new WrongValueException(aComp, msg);
			}
		}
	}

}
